﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Wulong.Phone.Qiushi.i18n;

namespace Wulong.Phone.Qiushi.Models
{
    [DataContract]
    public class QiushiItem : IComparable, IEqualityComparer<QiushiItem>
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string Content { get; set; }
        [DataMember]
        public string ImageUrl { get; set; }
        [DataMember]
        public string MobileImageUrl { get; set; }
        [DataMember]
        public string Tags { get; set; }
        [DataMember]
        public int Tops { get; set; }
        [DataMember]
        public int Flops { get; set; }
        [DataMember]
        public int CommentsCount { get; set; }
        [DataMember]
        public DateTime PublishDateTime { get; set; }
 
        public string Age
        {
            get
            {
                if (DateTime.Now.Subtract(PublishDateTime).Minutes < 10)
                    return AppResources.M_QiushiItem_TimeSpan_JustNow;
                if (DateTime.Now.Subtract(PublishDateTime).Minutes < 30)
                    return AppResources.M_QiushiItem_TimeSpan_Recently;
                if (DateTime.Now.Subtract(PublishDateTime).Minutes < 60)
                    return AppResources.M_QiushiItem_TimeSpan_Half_Hour_Ago;
                if (DateTime.Now.Subtract(PublishDateTime).Hours < 2)
                    return AppResources.M_QiushiItem_TimeSpan_An_Hour_Ago;
                if (DateTime.Now.Subtract(PublishDateTime).Hours < 3)
                    return AppResources.M_QiushiItem_TimeSpan_Two_Hours_Ago;
                if (DateTime.Now.Subtract(PublishDateTime).Hours < 24)
                    return AppResources.M_QiushiItem_TimeSpan_In_One_Day;
                if (DateTime.Now.Subtract(PublishDateTime).Days < 2)
                    return AppResources.M_QiushiItem_TimeSpan_One_Day_Ago;
                if (DateTime.Now.Subtract(PublishDateTime).Days < 3)
                    return AppResources.M_QiushiItem_TimeSpan_Two_Days_Ago;
                if (DateTime.Now.Subtract(PublishDateTime).Days < 7)
                    return AppResources.M_QiushiItem_TimeSpan_In_A_Week;
                return DateTime.Now.Subtract(PublishDateTime).Days < 30 ? AppResources.M_QiushiItem_TimeSpan_In_A_Month : AppResources.M_QiushiItem_TimeSpan_Long_Ago;
            }
        }

        int IComparable.CompareTo(object right)
        {
            if (!(right is QiushiItem)) throw new ArgumentException(AppResources.Ex_QiushiItem_Comparable_Argument_Exception, "right");
            QiushiItem righItem = (QiushiItem) right;
            return CompareTo(righItem);
        }

        public int CompareTo(QiushiItem right)
        {
            return Id.CompareTo(right.Id);
        }

        // Relational Operators.
        public static bool operator <(QiushiItem left, QiushiItem right)
        {
            return left.CompareTo(right) < 0;
        }
        public static bool operator <=(QiushiItem left, QiushiItem right)
        {
            return left.CompareTo(right) <= 0;
        }
        public static bool operator >(QiushiItem left, QiushiItem right)
        {
            return left.CompareTo(right) > 0;
        }
        public static bool operator >=(QiushiItem left, QiushiItem right)
        {
            return left.CompareTo(right) >= 0;
        }

        private static DateTimeComparer _datetimeComparer;
        public static IComparer DateTimeCompare
        {
            get
            {
                if (_datetimeComparer == null)
                    _datetimeComparer = new DateTimeComparer();
                return _datetimeComparer;
            }
        }

        private class DateTimeComparer : IComparer
        {
            #region IComparer Members
            int IComparer.Compare(object left, object right)
            {
                if (!(left is QiushiItem))
                    throw new ArgumentException(AppResources.Ex_QiushiItem_Comparable_Argument_Exception, "left");
                if (!(right is QiushiItem))
                    throw new ArgumentException(AppResources.Ex_QiushiItem_Comparable_Argument_Exception, "right");
                QiushiItem leftItem = (QiushiItem)left;
                QiushiItem rightItem = (QiushiItem)right;
                return leftItem.PublishDateTime.CompareTo(
                 rightItem.PublishDateTime);
            }
            #endregion

        }

        public bool Equals(QiushiItem x, QiushiItem y)
        {
            if (x.Id == y.Id)
                return true;
            return false;
        }

        public int GetHashCode(QiushiItem obj)
        {
            return obj.Id.GetHashCode();
        }
    }
}
